import 'package:flutter/material.dart';
import 'package:ninetyfour/l10n/app_localizations.dart';

typedef OrderShaiCallback = void Function(int index);
typedef FilterCallback = void Function();

class OrderShaiView extends StatefulWidget {
  final OrderShaiCallback clickShai;
  final FilterCallback clickSHide;
  final int selectData;

  const OrderShaiView({
    super.key,
    required this.clickShai,
    required this.clickSHide,
    required this.selectData,
  });

  @override
  _OrderShaiViewState createState() => _OrderShaiViewState();
}

class _OrderShaiViewState extends State<OrderShaiView> {
  int select = 1;
  int? selectedIndex = 0;

  @override
  void initState() {
    super.initState();
    selectedIndex = widget.selectData;
    select = widget.selectData + 1;
  }

  @override
  Widget build(BuildContext context) {
    final List<String> btns = [
      AppLocalizations.of(context)!.last1Month,
      AppLocalizations.of(context)!.last3Months,
      AppLocalizations.of(context)!.last6Months,
    ];

    final buttonWidth = (MediaQuery.of(context).size.width - 60) / 2;
    const buttonHeight = 41.0;

    return Stack(
      children: [
        // ✅ 半透明背景蒙层
        GestureDetector(
          onTap: widget.clickSHide,
          child: Container(
            color: Colors.black.withValues(alpha: 0.4),
            width: double.infinity,
            height: double.infinity,
          ),
        ),

        // ✅ 底部弹出内容
        Align(
          alignment: Alignment.bottomCenter,
          child: Container(
            width: double.infinity,
            decoration: BoxDecoration(
              color: Colors.white,
              borderRadius: const BorderRadius.only(
                topLeft: Radius.circular(12),
                topRight: Radius.circular(12),
              ),
              boxShadow: [
                BoxShadow(
                  color: Colors.black.withValues(alpha: 0.5),
                  offset: const Offset(0, 4),
                  blurRadius: 8,
                ),
              ],
            ),
            padding: const EdgeInsets.only(bottom: 20),
            child: Column(
              mainAxisSize: MainAxisSize.min,
              children: [
                Padding(
                  padding: const EdgeInsets.fromLTRB(15, 15, 15, 25),
                  child: Text(
                    AppLocalizations.of(context)!.filterByTime,
                    style: const TextStyle(
                      color: Colors.black,
                      fontSize: 16,
                    ),
                  ),
                ),
                Center(
                  child: Wrap(
                    alignment: WrapAlignment.start,
                    crossAxisAlignment: WrapCrossAlignment.center,
                    spacing: 20,
                    runSpacing: 16,
                    children: List.generate(btns.length, (index) {
                      return SizedBox(
                        width: buttonWidth,
                        height: buttonHeight,
                        child: ElevatedButton(
                          style: ElevatedButton.styleFrom(
                            backgroundColor: selectedIndex == index
                                ? const Color(0xFFF3F3FF)
                                : Colors.grey[200],
                            foregroundColor: selectedIndex == index
                                ? const Color(0xFF5D5FEE)
                                : Colors.grey[700],
                            shape: RoundedRectangleBorder(
                              borderRadius: BorderRadius.circular(20),
                            ),
                            elevation: 0,
                            padding: EdgeInsets.zero,
                          ),
                          onPressed: () {
                            setState(() {
                              selectedIndex = index;
                              select = index + 1;
                            });
                          },
                          child: Text(btns[index],
                              style: const TextStyle(fontSize: 13)),
                        ),
                      );
                    }),
                  ),
                ),
                Padding(
                  padding: const EdgeInsets.only(
                      top: 25, bottom: 10, left: 20, right: 20),
                  child: Row(
                    mainAxisAlignment: MainAxisAlignment.spaceBetween,
                    children: [
                      SizedBox(
                        width: buttonWidth,
                        height: buttonHeight,
                        child: OutlinedButton(
                          style: OutlinedButton.styleFrom(
                            side:
                                const BorderSide(color: Color(0xFF5D5FEE)),
                            shape: RoundedRectangleBorder(
                              borderRadius: BorderRadius.circular(20),
                            ),
                            foregroundColor: const Color(0xFF5D5FEE),
                          ),
                          onPressed: widget.clickSHide,
                          child: Text(
                              AppLocalizations.of(context)!.canceled),
                        ),
                      ),
                      SizedBox(
                        width: buttonWidth,
                        height: buttonHeight,
                        child: ElevatedButton(
                          style: ElevatedButton.styleFrom(
                            backgroundColor: const Color(0xFF5D5FEE),
                            foregroundColor: Colors.white,
                            shape: RoundedRectangleBorder(
                              borderRadius: BorderRadius.circular(20),
                            ),
                          ),
                          onPressed: () {
                            if (select != 0) {
                              widget.clickShai(select);
                            }
                          },
                          child:
                              Text(AppLocalizations.of(context)!.confirm),
                        ),
                      ),
                    ],
                  ),
                ),
              ],
            ),
          ),
        ),
      ],
    );
  }
}
